<?php
namespace app\common\model\cost;

use think\Model;
use think\facade\Session;
use app\common\model\user\User;

class Costtype extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = 'costtype';
        protected $pk = 'TypeId';
        
        const STATEOK  = 1;
        const STATEINT = 1;
        const STATEWAIT = 2;//失效
        const STATEDELETE = 404;//被删除的订单

    public static function screate($idata){
        $userinfo = User::getinfo();
        //判断是否重复
        if(empty($data['TypeName'])){
            return moerr('缺少费用分类名称');
        }
        $sb = Costtype::where('TypeName',$idata['TypeName'])->find();
        if(!empty($sb)){
            return moerr('费用分类名称重复');
        }
        //基础信息
        $data['State'] = Costtype::STATEINT;
        $data['TypeName'] = $idata['TypeName'];
        $data['TypeTable'] = $idata['TypeTable'];
        $data['TypeName'] = $idata['TypeName'];
        $data['UpUserId'] = $userinfo->UserId;
        $data['RoleMap'] = $idata['RoleMap'];
        //审批信息
        $data['ApprovalProcess'] = $idata['ApprovalProcess'];
        $data['ApprovalType'] = $idata['ApprovalType'];
        $data['CCList'] = $idata['CCList'];
        $ReceiptList = Costtype::create($data);
        return sok('已添加费用分类');
    }

     /**
     * 状态分类
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public static function getStatusText($value = '')
    {
        $status = [-1=>'全部',0=>'已生效',1=>'已失效'];
        if($value!==''){
            return $status[$value];
        }
        return $status;
    }

    /**
     * 获取分类列表
     * @return [type] [description]
     */
    public static function getlist($option='user',$TypeTable = ''){
        $data = Costtype::where('State',Costtype::STATEOK);
        if($option=='user'){
            $user = User::getinfo();
            $data = $data->where('RoleMap','like','%'.$user->Role.'%');
        }elseif($option=='accocreate'){
            if(!empty($TypeTable)){
                $data = $data->where('TypeTable',$TypeTable);
            }else{
                $data = $data->where('TypeTable','');
            }
        }elseif($option=='costview'){
            $visible = ['TypeName','TypeId'];
        }else{
            return moerr('参数错误');
        }
        $data = $data->select();
        if(empty($visible)){
            $data->hidden(['UpUserId','OperationLog']);
        }else{
            $data->visible($visible);
        }
        return $data;
    }

    /**
     * 获取关联订单列表
     * @param  [type] $id   [description]
     * @param  [type] $type [description]
     * @return [type]       [description]
     */
    public static function getcostlist($id,$type){
        $data = Costtype::where('TypeTable',$type)->find();
        if(empty($data)){
            return moerr('财务参数错误');
        }
        $rdata['pay'] = Pay::where('RelationListId',$id)->where('TypeId',$data->TypeId)->field('PayTime,PayCost,UpTime,State,PayId as id')->select();
        $rdata['revenue'] = Revenue::where('RelationListId',$id)->where('TypeId',$data->TypeId)->field('PayTime,PayCost,UpTime,State,RevenueId as id')->select();
        
        $rdata['statetext'] = Revenue::getStatusText();
        return $rdata;
    }

     /**
     * 获取订单信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getinfo($id,$type='costview'){
        $data = Costtype::get($id);
        if($type=='costview'){
            $data = $data->visible(['TypeId','TypeName','TypeTable']);
        }elseif($type=='mana'){
            //无需操作
        }else{
            return moerr('参数错误');
        }
        return $data; 
    }

    /**
     * 根据type获得他的id
     * @param  [type] $type [description]
     * @return [type]       [description]
     */
    public static function getid($type){
        if(empty($type)){
            return moerr('缺少参数');
        }
        $data = Costtype::where('TypeTable',$type)->find();
        if(empty($data)){
            return moerr('没找到对应的财务分类');
        }
        return $data->TypeId;
    }

    /**
    * 用户修改信息
    * @param  [type]  $data   [description]
    * @param  boolean $wtnote [是否记录]
    * @return [type]          [description]
    */
    public static function user_reinfo($data,$wtnote=true){
        if(empty($data['id'])){
            return moerr('缺少重要参数');
        }
        $info = Costtype::get($data['id']);
        $userinfo = User::getinfo();
        $reinfo = empty($info->OperationLog)?'':$info->OperationLog."\n";
        $reinfo = $reinfo .'【' .$userinfo->Name.'|'.$userinfo->UserId.'】在'.date('Y-m-d H:i',time()).'进行了信息调整：';
        $nreinfo = '';
        foreach ($data as $key => $value) {
            $sb = $info->getfiledname($key);
            if(empty($sb)){
                continue;
            }
            //取原数据
            $ovalue = $info->$key;
            if($key=='State'&&$value!==$ovalue){
                $info->$key = $value;
                //value将改变成文字所以提前赋值
                $value = $info->getStatusText((int)$value);
                $ovalue = $info->getStatusText((int)$ovalue);
                $nreinfo =  $nreinfo . '字段【'.$info->getfiledname($key).'】由【'.  $ovalue .'】修改为【' .$value.'】;'."\n";
            }else if($key=='DisposalSiteId'&&$value!==$ovalue){
                $info->$key = $value;
                $namedata = Costtype::getsitelistname([$value,$ovalue]);
                $value = $namedata[$value].'('.$value.')';
                $ovalue = $namedata[$ovalue].'('.$ovalue.')';
                $nreinfo =  $nreinfo . '字段【'.$info->getfiledname($key).'】由【'.  $ovalue .'】修改为【' .$value.'】;'."\n";
            }else if(!empty($value)&&$ovalue !== $value){
                $nreinfo =  $nreinfo . '字段【'.$info->getfiledname($key).'】由【'.  $ovalue .'】修改为【' .$value.'】;'."\n";
                $info->$key = $value;
            }
        }
        if(!empty($nreinfo)){
            if($wtnote){
                $reinfo = $reinfo ."\n" . $nreinfo;
                $info->OperationLog = $reinfo;
            }
            $info->save();
        }else{
            return moerr('没有找到需要调整的数据');
        }
    }

    public static function getfiledname($str){
        $filename = ['TypeName'=>'费用类型名称','TypeNote'=>'费用类型解释','TypeTable'=>'分类表名',
                 'State'=>'费用状态','RoleMap'=>'适用角色','ApprovalType'=>'审批流程',
                 'ApprovalProcess'=>'审批类型','CCList'=>'抄送人'];
        if(empty($filename[$str])){
            return false;
        }
        return $filename[$str];
    }
}
